package com.esisa.dao.hibernate;

import java.util.List;

import com.esisa.dao.RaceDao;
import com.esisa.dao.hibernate.AbstractHibernateDao;
import com.esisa.models.Race;

public class RaceDaoHibernate extends AbstractHibernateDao implements RaceDao {

	@Override
	public boolean insert(Race race) {
		beginTransaction();
		  Race c=(Race) session.get(Race.class,race.getNom());
		   if(c==null){
		   	session.save(race);
		   	return true;
		    }
		commit();
		return false;
	}

	@Override
	public List<Race> selectAll() {
		List<Race> list = null;  
		String req = " FROM Race r" ;
		beginTransaction();
		list = session.createQuery(req).list();
		commit();
		return list;
	}

	@Override
	public void delete(Race race) {
		beginTransaction();
			session.save(race);
		commit();
		
	}

	@Override
	public List<Race> selectRaceByType(String idType) {
		System.out.println("je susi a racedaoHibernate FindRaceSelonType");
		List<Race> list = null;
		String req= "From Race race where race.typeBetail.type = '" + idType + "'" ;
				
		beginTransaction();
		list = session.createQuery(req).list();
		for (int i = 0; i < list.size(); i++) {
			list.get(i).getTypeBetail();
		}
		
		commit();
		return list;
		
		
	}

	@Override
	public void update(Race race) {
		beginTransaction();
			session.update(race);
		commit();
	}
	
	

}
